package com.banciyuan.bcywebview.biz.detail.charge;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.view.dialog.WaitDialog;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.web.WebActivity;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.pingplusplus.android.Pingpp;

import org.json.JSONException;
import org.json.JSONObject;

import de.greenrobot.daoexample.model.ChargeItem;
import de.greenrobot.daoexample.model.UserDetail;

/**
 * Created by squall on 2016/5/16.
 */
public class ChargeSelectActivity extends BaseActivity implements View.OnClickListener {

    public static final int PAY_SUCC = 2135;
    public static final int PAY_FAIL = 1231;

    private TextView charge_text;
    private ImageView close_iv;
    private LinearLayout feed_1;
    private LinearLayout feed_5;
    private LinearLayout feed_10;
    private LinearLayout feed_48;
    private LinearLayout feed_95;
    private LinearLayout[] feeds;
    private TextView feed_tv;
    private TextView feed_tv_disable;
    private CheckBox check;
    private TextView check_tv;
    private TextView feed_arrgement;

    private String select_feed = "one";

    private String post_id;
    private String uid;
    private String type;
    private String content;
    private ChargeItem chargeItem;

    private WaitDialog waitDialog;

    private String charge;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.charge_select);
        initArgs();
        initUi();
        initAction();
    }

    @Override
    protected void initArgs() {
        Intent intent = getIntent();
        post_id = intent.getStringExtra("post_id");
        uid = intent.getStringExtra("uid");
        type = intent.getStringExtra("type");
        content = intent.getStringExtra("content");
        chargeItem = new ChargeItem();
        chargeItem.setPost_id(post_id);
        chargeItem.setPost_uid(uid);
        chargeItem.setPost_type(type);
    }

    @Override
    protected void initUi() {
        waitDialog = new WaitDialog(this, R.style.Dialog);
        charge_text = (TextView) findViewById(R.id.charge_text);
        close_iv = (ImageView) findViewById(R.id.close_iv);
        feed_1 = (LinearLayout) findViewById(R.id.feed_1);
        feed_5 = (LinearLayout) findViewById(R.id.feed_5);
        feed_10 = (LinearLayout) findViewById(R.id.feed_10);
        feed_48 = (LinearLayout) findViewById(R.id.feed_48);
        feed_95 = (LinearLayout) findViewById(R.id.feed_95);

        feed_1.setTag("one");
        feed_5.setTag("five");
        feed_10.setTag("ten");
        feed_48.setTag("fifty");
        feed_95.setTag("1hundred");

        feed_tv = (TextView) findViewById(R.id.feed_now);
        feed_tv_disable = (TextView) findViewById(R.id.feed_now_disable);
        check = (CheckBox) findViewById(R.id.check_cb);
        check_tv = (TextView) findViewById(R.id.check_tv);
        feed_arrgement = (TextView) findViewById(R.id.arrgement_tv);
        feeds = new LinearLayout[]{feed_1, feed_5, feed_10, feed_48, feed_95};
        check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    feed_tv.setVisibility(View.VISIBLE);
                    feed_tv_disable.setVisibility(View.GONE);
                } else {
                    feed_tv.setVisibility(View.GONE);
                    feed_tv_disable.setVisibility(View.VISIBLE);
                }
            }
        });
        if (!TextUtils.isEmpty(content)) charge_text.setText(content);
        selectFeed(1);
    }

    @Override
    protected void initAction() {
        close_iv.setOnClickListener(this);
        feed_1.setOnClickListener(this);
        feed_5.setOnClickListener(this);
        feed_10.setOnClickListener(this);
        feed_48.setOnClickListener(this);
        feed_95.setOnClickListener(this);
        feed_tv.setOnClickListener(this);
        check_tv.setOnClickListener(this);
        feed_arrgement.setOnClickListener(this);
    }

    @Override
    public void onBackPressed() {
        close();
    }

    private void close() {
        finish();
        overridePendingTransition(R.anim.umeng_socialize_fade_in, R.anim.umeng_socialize_fade_out);
    }

    private void selectFeed(int position) {
        for (int i = 0; i < feeds.length; i++) {
            if (position == i) {
                feeds[i].setBackgroundResource(R.drawable.shape_white_big_pink_edge);
                select_feed = (String) feeds[i].getTag();
            } else {
                feeds[i].setBackgroundResource(R.drawable.shape_white_big);
            }
        }
    }

    private void beginCharge() {
        chargeItem.setCharge_type(select_feed);
        PayHelper payhelper = new PayHelper(this);
        payhelper.getPayOrder(new PayHelperInter() {
            @Override
            public void Succ(String response) {
                try {
                    if (!ChargeSelectActivity.this.isFinishing()) waitDialog.dismiss();
                    JSONObject jsonobject = new JSONObject(response);
                    JSONObject result = jsonobject.getJSONObject(HttpUtils.UPLOAD_DATA);
                    charge = result.getString("charge");
                    Pingpp.createPayment(ChargeSelectActivity.this, charge);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void fail() {
                if (!ChargeSelectActivity.this.isFinishing()) waitDialog.dismiss();
            }
        }, chargeItem);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close_iv:
                close();
                break;
            case R.id.feed_1:
                selectFeed(0);
                break;
            case R.id.feed_5:
                selectFeed(1);
                break;
            case R.id.feed_10:
                selectFeed(2);
                break;
            case R.id.feed_48:
                selectFeed(3);
                break;
            case R.id.feed_95:
                selectFeed(4);
                break;
            case R.id.feed_now:
                if (!waitDialog.isShowing()) {
                    waitDialog.show();
                    beginCharge();
                }
                break;
            case R.id.arrgement_tv:
                gotoUtil.gotoAct(this, WebActivity.class, getString(R.string.arrgement));
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //支付页面返回处理
        if (requestCode == Pingpp.REQUEST_CODE_PAYMENT) {
            if (resultCode == Activity.RESULT_OK) {
                String result = data.getExtras().getString("pay_result");
                if (result.equals("success")) {
                    try {
                        JSONObject jsonObject = new JSONObject(charge);
                        getChargeUser(jsonObject.getString("order_no"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else if (result.equals("fail")) {
                    MyToast.show(this, getString(R.string.pay_fail));
                } else if (result.equals("cancel")) {
                    MyToast.show(this, getString(R.string.cancel_pay));
                }
            }
        }
    }

    private void getChargeUser(String order_no) {
        PayHelper payHelper = new PayHelper(this);
        payHelper.getPayStatus(new PayHelperInter() {
            @Override
            public void Succ(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONObject result = jsonObject.getJSONObject(HttpUtils.UPLOAD_DATA);
                    UserDetail userDetail = new UserDetail();
                    userDetail.setAvatar(result.getString("avatar"));
                    userDetail.setUid(result.getString("uid"));
                    Intent intent = new Intent();
                    intent.putExtra("userdetail", userDetail);
                    intent.putExtra("under_700", select_feed.equals("one") || select_feed.equals("five"));
                    setResult(PAY_SUCC, intent);
                    close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void fail() {
                setResult(PAY_FAIL);
                close();
            }
        }, order_no);
    }
}
